package com.tongyi.lizhishop.activitys;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.tongyi.lizhishop.beans.CommonResonseBean;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.utils.PromptDialogUtils;
import com.tongyi.lizhishop.managers.APIServiceManager;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;

import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.MessageApi;
import com.tongyi.lizhishop.common.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.leefeng.promptlibrary.PromptDialog;

public class AddIcCardActivity extends BaseActivity {

    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.bankNameEt)
    EditText bankNameEt;
    @BindView(R.id.bankNumberEt)
    EditText bankNumberEt;
    @BindView(R.id.bankOwnerNameEt)
    EditText bankOwnerNameEt;
    @BindView(R.id.phoneNumberEt)
    EditText phoneNumberEt;
    @BindView(R.id.addBankCardBtn)
    Button addBankCardBtn;
    @BindView(R.id.activity_add_ic_card)
    LinearLayout activityAddIcCard;
    private PromptDialog prompDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_ic_card);
        ButterKnife.bind(this);
        initTitleBarView(titlebar, "添加卡片");

        prompDialog = PromptDialogUtils.getPrompDialog(this);
    }

    @OnClick(R.id.addBankCardBtn)
    public void onViewClicked() {
        if (!checkParams()) {
            return;
        }
        prompDialog.showLoading("请等待");
        String bankName = bankNameEt.getText().toString();
        String bankNumber = bankNumberEt.getText().toString();
        String bankOwnerName = bankOwnerNameEt.getText().toString();
        String phoneNumber = phoneNumberEt.getText().toString();

        APIServiceManager.getInstance().create(MessageApi.class)
                .savemyBank(SPUtils.getInstance().getString("userid"), bankName, bankNumber, bankOwnerName, phoneNumber)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CommonObserver<CommonResonseBean>() {
                    @Override
                    public void onSuccess(CommonResonseBean value) {

                        LogUtils.w(value);

                        prompDialog.showSuccess("保存成功", false);
                        finish();
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                        prompDialog.showError("请检查网络", false);
                    }
                });


    }

    /**
     * 参数不符合返回false;
     *
     * @return
     */
    private boolean checkParams() {
        return true;
    }
}
